import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/model/event/event_bus.dart';
import 'package:lcapp/page/drawer_page.dart';
import 'package:lcapp/page/home/home_follow_page.dart';
import 'package:lcapp/page/home/home_video_page.dart';
import 'package:lcapp/widget/asr_app_bar.dart';
import 'package:lcapp/widget/custom_indicator.dart';
import 'package:lcapp/widget/custom_tabs.dart';
import 'package:lcapp/widget/lc_gesture_detector.dart';

class HomePage extends StatefulWidget {
  const HomePage({Key? key}) : super(key: key);

  @override
  _HomePageState createState() => _HomePageState();
}

int homeTabIndex = 1;
class _HomePageState extends State<HomePage> with SingleTickerProviderStateMixin,AutomaticKeepAliveClientMixin{
  TabController? tabController;
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    tabController = TabController(length: 2, vsync: this);
    tabController?.addListener(_tabListener);
    tabController?.index=homeTabIndex;
  }
  _tabListener(){
    setState(() {
      homeTabIndex = tabController!.index;
    });
    eventBus.fire(HomeChangeIndex(canPlay:true));
  }
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    tabController?.removeListener(_tabListener);
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: Stack(
        children: [
          Positioned(child: Container(
            width: 1.sw,
            height: 1.sh,
            child: TabBarView(
              controller: tabController,
              children: const [
                // HomeFlollowPage(),
                HomeVideoPage()
              ],
            )
          ),left: 0,right: 0,top:0,bottom: 0,),
          Positioned(
            child: AsrAppBar(
              backgroundColor: Colors.transparent,
              barColor: Colors.white,
              isLead: true,
              leadingWidth: 130.w,
              centerTitle: true,
              leading: lcGestureDetector(
                isLogin:true,
                onTap: (){
                  if(sliderDrawerKey!=null){
                    if(sliderDrawerKey.currentState!.isDrawerOpen){
                      // eventBus.fire(HomeChangeIndex(canPlay:true));
                      sliderDrawerKey.currentState!.closeSlider();
                      eventBus.fire(HomeSliderChangeIndex(isOpen:false));
                    }else{
                      eventBus.fire(HomeChangeIndex(canPlay:false));
                      sliderDrawerKey.currentState!.openSlider();
                      eventBus.fire(HomeSliderChangeIndex(isOpen:true));
                    }
                  }
                },
                child: Container(
                  color: Colors.transparent,
                  padding: EdgeInsets.only(left: 34.w,),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.start,
                    children: [
                      Image.asset("assets/home/home_menu.png",fit: BoxFit.fitWidth,width: 50.w,)
                    ],
                  ),
                ),
              ),
              actions: [
                lcGestureDetector(
                  isLogin:true,
                  onTap: (){
                    pushPageName("SearchPage");
                  },
                  child: Container(
                    width: 100.w,
                    color: Colors.transparent,
                    alignment: Alignment.centerRight,
                    padding: EdgeInsets.only(right: 30.w),
                    child: Image.asset("assets/home/home_search.png",width: 50.w,fit: BoxFit.fitWidth,),
                  ),
                )
              ],
              title: Container(
                // color: Colors.red,
                width: 380.w,
                child: TabBar(
                  controller: tabController!,
                  // indicator: CustomIndicator(
                  //     type: CustomIndicatorType.LineRect,
                  //     height: 2,
                  //     width: 30.w,
                  //     color: Colors.white),
                  labelColor: Colors.white,
                  labelStyle: TextStyle(
                    fontSize: 40.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w600,
                  ),
                  unselectedLabelColor: Colors.white,
                  unselectedLabelStyle: TextStyle(
                    fontSize: 30.sp,
                    color: Colors.white,
                    fontWeight: FontWeight.w400,
                  ),
                  tabs: const [
                    Text("关注"),
                    Text("视频"),
                  ],
                ),
              ),
            ),
            left: 0,right: 0,top: 0,
          ),
        ],
      ),
    );
  }

  pushPageName(String name){
    eventBus.fire(HomeChangeIndex(canPlay:false));
    Navigator.of(context).pushNamed(name).then((value){
        eventBus.fire(HomeChangeIndex(canPlay:true));
    });
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
